package com.diyunapp.xilinapp.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.user_icon)
    ImageView userIcon;
    @Bind(R.id.userlogin_et)
    EditText userloginEt;
    @Bind(R.id.user_password_et)
    EditText userPasswordEt;
    @Bind(R.id.login_tv)
    TextView rechargeTv;
    @Bind(R.id.register_bn)
    Button getmoneyTv;
    @Bind(R.id.forget_pass)
    TextView forgetPass;

    private SharedPreferences sharedPreferences;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        View headView = findViewById(R.id.login_menubar_inc);
        super.MenuWidget(headView);
        setResult(0);
    }



    @OnClick({R.id.go_back,R.id.login_tv,R.id.register_bn,R.id.forget_pass})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.go_back:
                finish();
            break;
            case R.id.login_tv:
                if (userloginEt.getText().toString().trim()==""){
                    toast("用户名不能为空");
                }else if(userPasswordEt.getText().toString().trim()==""){
                    toast("密码不能为空");
                }else{
                    //登录
                    login();
                }
                break;
            case R.id.register_bn:
                nextAct(ResgiterFirActivity.class);
                break;
            case R.id.forget_pass:

                nextAct(ForPassFirActivity.class);
                break;
        }

    }

    public void login() {
        String username=userloginEt.getText().toString().trim();
        String password=userPasswordEt.getText().toString().trim();
        String url=""+MainActivity.url+"act=login&" +
                "username="+username+"&password="+password+"&client=android";
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST,url,
                new RequestCallBack<String>()
                {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading)
                    {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo)
                    {
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");
                            JSONObject datas = resultObject.optJSONObject("datas");
                            if(result==200)
                            {
                                String error = datas.optString("error");
                                if (error!="")
                                {
                                    toast(error);
                                }
                                else
                                {
                                    String username=datas.optString("username");
                                    String member_id=datas.optString("member_id");
                                    String key=datas.optString("key");
                                    String member_avatar=datas.optString("member_avatar");
                                    SharedPreferences user_info=getSharedPreferences("xilinshangchengSP",MODE_PRIVATE);
                                    user_info.edit().putString("username",username).commit();
                                    user_info.edit().putString("member_id",member_id).commit();
                                    user_info.edit().putBoolean("haveLogin",true).commit();
                                    user_info.edit().putString("key",key).commit();
                                    user_info.edit().putString("member_avatar",member_avatar).commit();
                                    MainActivity.myLogin.setUpdate_head(false);
                                    MainActivity.myLogin.setUsername(username);
                                    MainActivity.myLogin.setLoginkey(key);
                                    MainActivity.myLogin.setLoginState(true);
                                    MainActivity.myLogin.setMemberid(member_id);
                                    MainActivity.myLogin.setHeadimge(member_avatar);
                                    MainActivity.myLogin.setIfneed_fresh(true);
                                    toast("欢迎"+username);
                                    setResult(1);
                                    finish();
                                }
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg)
                    {
                        toast("请求失败");
                    }
                });
    }
    public void nextAct(Class act){
        Intent intent = new Intent();
        intent.setClass(this, act);
        startActivity(intent);
    }
}
